#!/bin/bash

# 生成so文件
gcc -fPIC -I $JAVA_HOME/include -I $JAVA_HOME/include/linux -shared -o native/libHelloJNI.so native/HelloJNI.c;
gcc -fPIC -I $JAVA_HOME/include -I $JAVA_HOME/include/linux -shared -o native/libhello.so native/hello.c;

# absolute-path
path=$(dirname `readlink -f $0`)
echo "${path}"

# server name
name=$path/jni-jna-web-2.3.0.RELEASE.jar
echo $name

# log-directory
log_dir=$path"/logs"
if [ ! -d "$log_dir" ]; then
  mkdir $log_dir
fi

# shellcheck disable=SC2006
pid=`ps -ef | grep $name| grep -v grep |awk '{print $2}'`
if [ $pid ]; then
  echo $name is running pid=$pid
  read -r -p "restart the server? [y/n] " input
  case $input in
    [yY][eE][sS]|[yY])
    kill -9 $pid
    echo restart $name......
    sleep 3                
    nohup java -Xbootclasspath/a:$path -jar -Xms128m -Xmx256m $name --logging.config=logback-boot.xml >>$log_dir/info.log 2>&1 &
    tail -f $log_dir/info.log
    ;;

    [nN][oO]|[nN])
      echo "No"
      exit 1
      ;;
    *)
      echo "Invalid input Please enter [y/n]"
      echo ""
      exit 1
      ;;
esac
else
  nohup java -Xbootclasspath/a:${path} -jar -Xms128m -Xmx256m $name --logging.config=logback-boot.xml >>$log_dir/info.log 2>&1 &
  tail -f $log_dir/info.log
fi
